package com.pug.qywx.utils;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * @author RenGaoshuai
 * @date 2023/8/18 15:00
 * @description
 */
public class WorkWeixinUtil {

    public static final String default_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=92acfef4-b825-46e4-a70b-6c7266c2c03b";
    public static final Map<String, String> default_heads = new HashMap<>();
    static {
        default_heads.put("Content-Type", "application/json;charset=UTF-8");
    }

    /**
     * 发送markdown格式消息
     *
     * @param url
     * @param context
     */
    public static void sendMDMessage(String url, String context) {
        sendMessage(url, "markdown", context);
    }

    /**
     * 发送企业微信你机器人消息
     *
     * @param url
     * @param msgtype
     * @param context
     */
    public static void sendMessage(String url, String msgtype, String context) {

        Map<String, String> bodyMap = new HashMap<>();
        bodyMap.put("msgtype", msgtype);
        bodyMap.put(msgtype, context);

        HttpResponse execute = HttpUtil.createPost(url)
                .headerMap(default_heads, false)
                .body(JSON.toJSONString(bodyMap, true))
                .timeout(5 * 60 * 1000)
                .execute();

        System.out.println(execute);
    }
}
